package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.BspsQrCodeInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsQrCodeInfoMapperImpl;
import cc.rengu.igas.bsps.common.entity.QrCodeInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.request.MchntQrcodeQueryRequest;
import cc.rengu.igas.bsps.facade.response.MchntQrcodeQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

/**
 * @author zhangxuran
 * @version 1.0
 * @date 2020.07.22
 * 二维码对应商户查询
 */
public class BspsMchntQrCodeQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntQrcodeQueryRequest mchntQrcodeQueryRequest = new MchntQrcodeQueryRequest();
        ConvertUtil.convertOutput(mchntQrcodeQueryRequest);
        return mchntQrcodeQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {

        MchntQrcodeQueryRequest mchntQrcodeQueryRequest = (MchntQrcodeQueryRequest) request;
        String instId = mchntQrcodeQueryRequest.getHeader().getInstId();
        String qrUrl = mchntQrcodeQueryRequest.getQrUrl();
        if (StringUtil.isEmptyOrNull(qrUrl) || StringUtil.isEmptyOrNull(instId)) {
            rglog.error("必填性校验失败!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        MchntQrcodeQueryResponse mchntQrcodeQueryResponse = new MchntQrcodeQueryResponse();
        /*查询商户*/
        BspsQrCodeInfoMapper qrCodeInfoMapper = new BspsQrCodeInfoMapperImpl();
        QrCodeInfo qrCodeInfo = qrCodeInfoMapper.selectBspsQrCodeInfoByPrimaryKey(instId, qrUrl);
        if (qrCodeInfo == null || !"00".equals(qrCodeInfo.getQrStatus())) {
            rglog.error("二维码状态异常");
            throw new BizException(RespCodeEnum.INVALID_QRCODE_ERROR.getRespCode(), RespCodeEnum.INVALID_QRCODE_ERROR.getRespDesc());
        } else {
            mchntQrcodeQueryResponse.setMchntNo(qrCodeInfo.getMchntNo());
        }

        /* 重组返回结果 */
        BizResponse<MchntQrcodeQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(mchntQrcodeQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntQrcodeQueryResponse mchntQrcodeQueryResponse = (MchntQrcodeQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntQrcodeQueryResponse);
    }


}
